import logging

from pylons import request, response, session,config, tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from kooka.lib.base import BaseController, render
from kooka.lib.controllerhelper import processconfig
import kooka.model.meta as meta
from kooka.model import NewsCategory, News, News_Config, ExportConfig, init_model

from sqlalchemy import or_, and_, not_
from sqlalchemy import engine_from_config

from pylons.i18n.translation import set_lang
log = logging.getLogger(__name__)

class FrontController(BaseController):
    
    def __before__(self):
        set_lang(session.get('lang','zh'))
         #change the db
        if session.get('lang','zh') =='zh':
            engine = engine_from_config(config, 'sqlalchemy.zh.')
            init_model(engine)
        elif session.get('lang','zh') =='ja':
            engine = engine_from_config(config, 'sqlalchemy.ja.')
            init_model(engine)
        elif session.get('lang','zh')=='en':
            engine = engine_from_config(config, 'sqlalchemy.en.')
            init_model(engine)


    def __after__(self):
	response.headerlist.append(('P3P','CP=CAO PSA OUR'))

    def index(self,page,id=None,categoryid=None,pageindex='1'):
        processconfig(page, id,categoryid, int(pageindex))
        return render('/front/derived/%s.html' % page)

